# Create your views here.
from django.shortcuts import render_to_response
from facultyAdministration.models import Questions,Quiz 
from httplib import HTTPResponse
from django.http import HttpResponse
from django.template.context import RequestContext
import datetime
#def view_all(request):    
#    info = Question.objects.all()
#    
#    return render_to_response('start.html', {'info': info})

info={}

def db_questions(request,quiz_id):
    if not info:
        print "came to info"
        inf = Questions.objects.filter(quiz_id=quiz_id)
        for q in inf:
            info[q.id]=q
    request.session['number']=1
    return render_to_response('start.html',{'q':info[1]},RequestContext(request))
    
def db_next(request):
    if not info:
            print "came to info"
            inf = Questions.objects.all()
            for q in inf:
                info[q.id]=q
    if 'next' in request.POST.keys():
        number=1
        if request.session.get('number'):
            number=int(request.session['number'])+1
        if number>len(info):
            del request.session['number']
            number=number-1
        request.session['number']=number    
        return render_to_response('start.html', {'q': info[number]},RequestContext(request))
    elif 'prev' in request.POST.keys():
        number=1
        if request.session.get('number'):
            number=int(request.session['number'])-1
        if number<1:
            del request.session['number']
            number=number+1
        request.session['number']=number    
        return render_to_response('start.html', {'q': info[number]},RequestContext(request))
        
    elif 'save' in request.POST.keys():
        return HttpResponse("clicked save")    

def index(request):
    questions=Quiz.objects.filter(quiz_start_date_time__gte=datetime.datetime.now()).filter(quiz_end_date_time__lte=datetime.datetime.now())[:1]
    if questions:
        return render_to_response('start.html',{'quiz_id':questions.id})
    else:
        return HttpResponse("wait for quiz")
    
    
    
    